package com.dtream.easycode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dtream.easycode.dao.TbItemMapper;
import com.dtream.easycode.entity.TbItem;
import com.dtream.easycode.service.TbItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 商品表(TbItem)表服务实现类
 *
 * @author makejava
 * @since 2021-01-07 11:27:47
 */
@Service("tbItemService")
public class TbItemServiceImpl implements TbItemService {
    @Resource
    private TbItemMapper tbItemMapper;

    @Override
    public Page<TbItem> queryItem(TbItem tbItem) {
        Page<TbItem> tbItemPage = new Page<>(1, 20, true);
        QueryWrapper<TbItem> queryWrapper = new QueryWrapper();
        if (Objects.nonNull(tbItem.getId()) && tbItem.getId() > 0) {
            queryWrapper.lambda().eq(TbItem::getId, tbItem.getId());
        }
        return tbItemMapper.selectPage(tbItemPage, queryWrapper);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TbItem queryById(Long id) {
        return this.tbItemMapper.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param tbItem 实例对象
     * @return 实例对象
     */
    @Override
    public TbItem insert(TbItem tbItem) {
        this.tbItemMapper.insert(tbItem);
        return tbItem;
    }

    /**
     * 修改数据
     *
     * @param tbItem 实例对象
     * @return 实例对象
     */
    @Override
    public TbItem update(TbItem tbItem) {
        this.tbItemMapper.update(tbItem);
        return this.queryById(tbItem.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.tbItemMapper.deleteById(id) > 0;
    }
}